#ifndef _JOYSTICK_H_
#define _JOYSTICK_H_

#include "math/vec3.h"

#include "../sleipner_config.h"

#ifdef _SDL2
    #include "SDL2/include/SDL.h"
#else
    #include "SDL-1.2.13/include/SDL.h"
#endif

#include <vector>

namespace input
{
    class JoystickListener;
    
    // http://www.libsdl.org/docs/html/guideinput.html
    class Joystick
    {
    public:
        
        
        enum AXIS
        {
            AXIS_X,
            AXIS_Y,
            AXIS_Z,
            AXIS_C
            
        };
        
        static	Joystick&				Instance();
        
        
        bool							Tick(float dt);
        
        // update motions
        void							OnTilt(int Axis,float Value);
        
        
        /// add a listener
        void							AddListener(JoystickListener* _pListener);
        void							DropListener(JoystickListener* _pListener);
        
        void							JoystickDisable();
        void							JoystickEnable();
        
        
        const math::Vec3&					GetAxisState() { return m_AxisState; };
        
    private:
        Joystick();
        ~Joystick();
        /// find what index this listener resides at
        int								GetListenerIndex(JoystickListener* _pListener);
        std::vector<JoystickListener*>	m_Listeners;
        
        // track mouse info
        math::Vec3						m_AxisState;
        SDL_Joystick*					m_pJoystick;
        bool                            m_EventRecived;
        
        
    };
    
    class JoystickListener
    {
    public:
        /// will automatically register itself with the mouse handler \n
        /// IMPORTANT that you do this!
        JoystickListener();
        /// will automatically de-register itself with the keyboard handler
        virtual				~JoystickListener();
        /// key was pressed this frame
        virtual	void		OnJoystickTilt(const math::Vec3& AxisState) {};
        
        
        
        Joystick*			GetJoystick() const { return m_pJoystick; };
        void				Disable()	{ m_IsEnabled = false; };
        void				Enable()	{ m_IsEnabled = true; };
        bool				IsEnabled() const { return m_IsEnabled; };
    private:
        bool				m_IsEnabled;
        Joystick*			m_pJoystick;
    };
    
    
}  //namespace input

#define GET_JOYSTICK() input::Joystick::Instance()

#endif // _JOYSTICK_H_